package com.wallpaper;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.WallpaperManager;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class WallpaperActivity extends Activity {
	/** Called when the activity is first created. */
	// @Override
	// public void onCreate(Bundle savedInstanceState) {
	// super.onCreate(savedInstanceState);
	// setContentView(R.layout.main);
	//
	// ImageButton next = (ImageButton) findViewById(R.id.next);
	// next.setImageResource(R.drawable.wallpaper);
	// next.setOnClickListener(new View.OnClickListener() {
	//
	// @Override
	// public void onClick(View v) {
	// ImageView imageView = (ImageView) findViewById(R.id.imageView1);
	// imageView.setImageResource(R.drawable.wallpaper1);
	//
	// }
	// });
	//
	// ImageButton pre = (ImageButton) findViewById(R.id.Pre);
	// pre.setImageResource(R.drawable.wallpaper1);
	// pre.setOnClickListener(new View.OnClickListener() {
	//
	// @Override
	// public void onClick(View v) {
	// ImageView imageView = (ImageView) findViewById(R.id.imageView1);
	// imageView.setImageResource(R.drawable.wallpaper);
	//
	// }
	// });
	// ImageView imageView = (ImageView) findViewById(R.id.imageView1);
	// imageView.setMaxHeight(300);
	// Log.d(this.toString(), imageView.toString());
	// imageView.setImageResource(R.drawable.wallpaper);
	//
	// }

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		final Gallery gallery = (Gallery) findViewById(R.id.gallery);
		gallery.setSpacing(1);
		gallery.setPadding(0, 0, 0, 0);
		gallery.setAdapter(new MyAdapter(getApplicationContext()));

		final ImageView view = (ImageView) findViewById(R.id.imageView);

		SimpleGestureDetector listener = new SimpleGestureDetector(gallery);
		listener.setView(view);
		final GestureDetector detector = new GestureDetector(listener);

		gallery.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View arg0, MotionEvent event) {
				detector.onTouchEvent(event);
				return false;
			}
		});

		view.setOnLongClickListener(new View.OnLongClickListener() {

			@Override
			public boolean onLongClick(View arg0) {
				Log.d("LongClick", "onLongClick");
				AlertDialog.Builder builder = new Builder(
						view.getContext());
				builder.setMessage("Set to Wallpaper?");
				builder.setPositiveButton("Yes", new OnClickListener() {

					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						try {
							WallpaperManager.getInstance(view.getContext())
									.setResource(
											MyAdapter.myImageIds[gallery
													.getSelectedItemPosition()]);
						} catch (IOException e) {
							e.printStackTrace();
						}
					}

				});
				builder.create().show();
				return false;
			}
		});
		// final ViewFlipper flipper = new ViewFlipper(getApplicationContext());
		//
		// flipper.addView(ImageViewFactory.getInstance(getApplicationContext())
		// .createView(R.drawable.wallpaper),0);
		// flipper.addView(ImageViewFactory.getInstance(getApplicationContext())
		// .createView(R.drawable.wallpaper1),1);
		//
		//
		// final GestureDetector detector = new GestureDetector(new
		// SimpleGestureDetector(flipper));
		//
		// flipper.setOnTouchListener(new View.OnTouchListener() {
		//
		// @Override
		// public boolean onTouch(View v, MotionEvent event) {
		// detector.onTouchEvent(event);
		// return true;
		// }
		// });

		// flipper.setON

	}

	static class ImageViewFactory {

		private static Map<Context, ImageViewFactory> instances = new HashMap<Context, ImageViewFactory>();
		private Context ctx;

		private ImageViewFactory(Context ctx) {
			this.ctx = ctx;

		}

		public static ImageViewFactory getInstance(Context ctx) {
			if (!instances.containsKey(ctx)) {
				ImageViewFactory instance = new ImageViewFactory(ctx);
				instances.put(ctx, instance);
			}
			return instances.get(ctx);
		}

		public ImageView createView(int resourceId) {
			ImageView view = new ImageView(this.ctx);
			view.setImageResource(resourceId);
			return view;

		}

	}

	static class MyAdapter extends BaseAdapter {

		static int[] myImageIds = { R.drawable.wallpaper,
				R.drawable.wallpaper1, R.drawable.mm };
		private Context ctx;
		private int mGalleryItemBackground;

		public MyAdapter(Context ctx) {
			this.ctx = ctx;

			// TypedArray a =
			// ctx.obtainStyledAttributes(R.styleable.HelloGallery);
			// Log.e("hehe", a.toString());
			// mGalleryItemBackground = a.getResourceId(
			// R.styleable.HelloGallery_android_galleryItemBackground, 0);
			// a.recycle();
		}

		@Override
		public int getCount() {
			return myImageIds.length;
		}

		@Override
		public Object getItem(int arg0) {
			return arg0;
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView i = new ImageView(this.ctx);

			i.setImageResource(myImageIds[position]);
			i.setLayoutParams(new Gallery.LayoutParams(96, 160));
			i.setScaleType(ImageView.ScaleType.FIT_CENTER);
			i.setBackgroundResource(R.drawable.icon);
			return i;
		}

	}
}